import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
declare var Viewer: any;
@Component({
	selector: 'sa-repaired-record',
	templateUrl: './repaired-record.component.html',
	styleUrls: ['./repaired-record.component.css']
})
export class RepairedRecordComponent implements OnInit {
	tableData = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	tableParam: any = {
		//	    searchKey: '', //搜索keyword
		startTime: '',//查询开始时间参数
		endTime: '',//查询结束时间参数
		repairedStatus: '',//查询故障状态参数
		useTypeL1: '',//车辆分类一级
		useTypeL2: '',//车辆分类二级
		deviceId: '',//查询设备参数
		carNumber: '',//查询车牌号码参数
		companyName: '',//查询公司参数
		companyId: '',//查询公司参数
		repairedUser: '',//查询维修人
		export: '0',//是否导出  0:否  1:是
		faultType: '', //故障项目
		subFaultType: '' //故障类型
	}
	userList: any = [];//用户数据List
	userName: string = '';//查询用户参数
	userId: string = '';//查询用户id参数

	modal: any = { //图片弹窗
		picture: '',
		active: '',   //激活的图片
		activeNum: null,  //激活的图片下标
	};
	inputCompanyTreeCarData: any = {};//车辆运行状态模块跳转该页面传递过来的参数
	// 设置厂区在线的参数
	onlineSetCompanyId: string = ""; // 公司
	onlineSetCompanyName: string = ""; // 公司
	factoryOnlinePageSize: number = 10; // 厂区在线表的页的条数
	factoryOnlineCurPage: any = 1;      // 当前页码
	factoryOnlinetotalCount: number = 0; // 分页总条数
	factoryOnlineSearchKey: string = ""; // 关键字搜素
	factoryOnlineTableData: any = []; // 厂区设置在线的表单数据
	selfTimeInputId: string = ''; //自定义时间段input的id值
	selfTimeData: any = []; //自定义时间段时间校验缓存集合
	startTimeSelected: number;
	endTimeSelected: number;
	carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
		this.queryRepaire('')
		this.load();
	}
	faultTypeList: any = []; //故障类型一级列表
	subFaultTypeList: any = []; //故障类型二级列表
	//查询故障类型
	queryRepaire(id) {
		let idValue
		if(id){
			idValue = {id: id}
		}else{
			idValue = {}
		}
		this.jsonApiService.http_obj("sanitationFaultType/list.html", idValue).subscribe(result => {
			if (result.success) {
				if (id) {
					this.subFaultTypeList = result.data;
				} else {
					this.faultTypeList = result.data;
				}

			}
		})
	}
	//更改故障项目
	changeFaultType(id) {
		this.tableParam.subFaultType = ''
		let faultType = this.faultTypeList.filter(function (item){ 
			if(item.type === id ){
				return item
			}
		})
		if(id) this.queryRepaire(faultType[0].id) //请求二级目录
	}
	ngOnDestroy() { }
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	}
	// 维修人删除
	delRepairedUser() {
		this.tableParam.repairedUser = '';
	}
	//table刷新
	refresh() {
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.tableParam.searchKey = common.stringTrim(this.tableParam.searchKey);
		this.load();
	}
	// 车辆分类编码转化   
	getUseTypeNameL2(key) {
		if (key) {
			return this.carDeatailUseList[key];
		} else {
			return '';
		}
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				startTime: this.tableParam.startTime,//查询开始时间参数
				endTime: this.tableParam.endTime,//查询结束时间参数
				status: this.tableParam.repairedStatus,//查询故障状态参数
				deviceId: this.tableParam.deviceId,//查询设备参数
				companyId: this.tableParam.companyId,//查询公司参数
				repairedUser: this.tableParam.repairedUser,//查询维修人
				faultType: this.tableParam.faultType,//故障项目
				subFaultType: this.tableParam.subFaultType,//故障类型
				export: '0',//是否导出  0:否  1:是
			},
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("sanitation/deviceRepaired/sanitationDeviceRepairedPage.html", reqdata).subscribe(result => {
			this.tableData = result.exhibitDatas || [];
			for (let item of this.tableData) {
				item.useTypeL2 = this.carDeatailUseList[item.useTypeL2] || '';
				//			    item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
			}
			this.totalCount = result.recordCount;
		})
	}
	// 导出
	table_export() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				startTime: this.tableParam.startTime,//查询开始时间参数
				endTime: this.tableParam.endTime,//查询结束时间参数
				status: this.tableParam.repairedStatus,//查询故障状态参数
				deviceId: this.tableParam.deviceId,//查询设备参数
				companyId: this.tableParam.companyId,//查询公司参数
				repairedUser: this.tableParam.repairedUser,//查询维修人
				faultType: this.tableParam.faultType,//故障项目
				subFaultType: this.tableParam.subFaultType,//故障类型
				export: '1',//是否导出  0:否  1:是
			},
		}
		let timeCheck = common.correctTime(data.parameters, ['export'])
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(data, "gridPager", config.BaseURL + 'sanitation/deviceRepaired/sanitationDeviceRepairedPage.html');
	}

	// 维修明细查看
	repairedDetailList(row) {
		this.router.navigate(['/installation-management/repaired-record/repaired-detail-list'], { queryParams: { id: row.id, type: 'updata', nowPage: this.curPage } });
	}
	// 维修历史
	repairedDetailPage: any = [];
	repaireHistory(row, modal) {
		modal.show();
		this.jsonApiService.http_obj("sanitation/deviceRepaired/sanitationDeviceRepairedHistory.html", { id: row.id }).subscribe(result => {
			if (result.success) {
				this.repairedDetailPage = result.data;
			}
		})
	}
	closeRepairedDetail(modal) {
		modal.hide();
	}
	repairedMonkeyPage: any = [];
	// 维修费用明细
	repairedMonkey(modal, row) {
		modal.show();
		this.jsonApiService.http_obj("sanitation/deviceRepaired/sanitationDeviceRepairedHistory.html", { id: row.id }).subscribe(result => {
			if (result.success) {
				this.repairedMonkeyPage = result.data;
			}
		})
	}
	closeRepairedMonkey(modal) {
		modal.hide();
	}

	//公司选中回调
	companySelected(data) {
		this.tableParam.companyId = data.companyId;
		this.tableParam.companyName = data.companyName;
		this.tableParam.deviceId = data.deviceId;
		this.tableParam.carNumber = data.carNumber;
		this.tableParam.useTypeL2 = data.useTypeL2;
		this.tableParam.useTypeL1 = data.useTypeL1;
	}

	// 新增删除功能
	deleteRow(row) {
		this.jsonApiService.confirm_message('删除维修记录会同步永久删除维修明细，确认要删除?').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("sanitation/deviceRepaired/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
}